package com.ruoyi.file.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.file.domain.FileDictData;
import com.ruoyi.file.mapper.FileDictDataMapper;
import com.ruoyi.utils.FileDictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.file.mapper.FileDictTypeMapper;
import com.ruoyi.file.domain.FileDictType;
import com.ruoyi.file.service.IFileDictTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件字典类型Service业务层处理
 * 
 * @author rww
 * @date 2023-02-20
 */
@Service
public class FileDictTypeServiceImpl implements IFileDictTypeService 
{
    @Autowired
    private FileDictTypeMapper fileDictTypeMapper;
    @Autowired
    private FileDictDataMapper fileDictDataMapper;

    /**
     * 查询文件字典类型
     * 
     * @param dictId 文件字典类型主键
     * @return 文件字典类型
     */
    @Override
    public FileDictType selectFileDictTypeByDictId(Long dictId)
    {
        return fileDictTypeMapper.selectFileDictTypeByDictId(dictId);
    }

    /**
     * 查询文件字典类型列表
     * 
     * @param fileDictType 文件字典类型
     * @return 文件字典类型
     */
    @Override
    public List<FileDictType> selectFileDictTypeList(FileDictType fileDictType)
    {
        return fileDictTypeMapper.selectFileDictTypeList(fileDictType);
    }

    /**
     * 新增文件字典类型
     * 
     * @param fileDictType 文件字典类型
     * @return 结果
     */
    @Override
    public int insertFileDictType(FileDictType fileDictType)
    {
        fileDictType.setCreateTime(DateUtils.getNowDate());
        return fileDictTypeMapper.insertFileDictType(fileDictType);
    }


    @Override
    public String checkDictTypeUnique(FileDictType dict)
    {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        FileDictType dictType = fileDictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    @Override
    @Transactional
    public int updateDictType(FileDictType dict)
    {
        FileDictType oldDict = fileDictTypeMapper.selectFileDictTypeByDictId(dict.getDictId());
        fileDictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = fileDictTypeMapper.updateFileDictType(dict);
        if (row > 0)
        {
            List<FileDictData> dictDatas = fileDictDataMapper.selectDictDataByType(dict.getDictType());
            FileDictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
        return row;
    }


    /**
     * 修改文件字典类型
     * 
     * @param fileDictType 文件字典类型
     * @return 结果
     */
    @Override
    public int updateFileDictType(FileDictType fileDictType)
    {
        fileDictType.setUpdateTime(DateUtils.getNowDate());
        return fileDictTypeMapper.updateFileDictType(fileDictType);
    }

    /**
     * 批量删除文件字典类型
     * 
     * @param dictIds 需要删除的文件字典类型主键
     * @return 结果
     */
    @Override
    public int deleteFileDictTypeByDictIds(Long[] dictIds)
    {
        return fileDictTypeMapper.deleteFileDictTypeByDictIds(dictIds);
    }

    /**
     * 删除文件字典类型信息
     * 
     * @param dictId 文件字典类型主键
     * @return 结果
     */
    @Override
    public int deleteFileDictTypeByDictId(Long dictId)
    {
        return fileDictTypeMapper.deleteFileDictTypeByDictId(dictId);
    }


    @Override
    public void resetDictCache()
    {
        clearDictCache();
        loadingDictCache();
    }

    @Override
    public void clearDictCache()
    {
        FileDictUtils.clearDictCache();
    }
    @Override
    public void loadingDictCache()
    {
        FileDictData dictData = new FileDictData();
        dictData.setStatus("0");
        Map<String, List<FileDictData>> dictDataMap = fileDictDataMapper.selectFileDictDataList(dictData).stream().collect(Collectors.groupingBy(FileDictData::getDictType));
        for (Map.Entry<String, List<FileDictData>> entry : dictDataMap.entrySet())
        {
            FileDictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(FileDictData::getDictSort)).collect(Collectors.toList()));
        }
    }

    @Override
    public List<FileDictType> selectDictTypeAll()
    {
        return fileDictTypeMapper.selectDictTypeAll();
    }


    @Override
    public List<FileDictData> selectDictDataByType(String dictType)
    {
        List<FileDictData> dictDatas = FileDictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = fileDictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            FileDictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }
}
